package com.fly.caihong.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fly.caihong.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 带有RecyclerView的Google SwipeRefreshLayout控件
 */
public class PullRefreshRecyclerView extends SwipeRefreshLayout {

    private static final int[] SCHEME_COLORS = {0XFF9C0248, 0XFF9C0248
            , 0XFF9C0248, 0XFF9C0248, 0XFF9C0248};
    private static final int CIRCLE_BG_LIGHT = 0xFFFAFAFA;
    private Context mContext;
    private RecyclerView mRecyclerView;
    private boolean isQuickAdapter;

    /**
     * 构造函数
     *
     * @param context
     */
    public PullRefreshRecyclerView(@NonNull Context context) {
        super(context);
        mContext = context;
        initRefreshStyle();
        initRecyclerView();
    }

    /**
     * xml构造函数
     *
     * @param context
     */
    public PullRefreshRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initRefreshStyle();
        initRecyclerView();
    }

    /**
     * 初始化刷新样式
     */
    private void initRefreshStyle() {
        setAlpha(1.0f);
        setBackgroundColor(CIRCLE_BG_LIGHT);
        setColorSchemeColors(SCHEME_COLORS);
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        LayoutInflater.from(mContext).inflate(R.layout.layout_recycler_view, this, true);
        mRecyclerView = findViewById(R.id.rv_recycler);
    }

    /**
     * 设置数据适配器，默认垂直线性布局
     *
     * @param adapter
     */
    public void setAdapter(RecyclerView.Adapter adapter) {
        setAdapter(adapter, new LinearLayoutManager(mContext));
    }

    /**
     * 设置数据适配器
     *
     * @param adapter
     * @param layoutManager
     */
    public void setAdapter(RecyclerView.Adapter adapter, RecyclerView.LayoutManager layoutManager) {
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
        isQuickAdapter = (adapter instanceof BaseQuickAdapter);
        if (isQuickAdapter) {
            BaseQuickAdapter quickAdapter = (BaseQuickAdapter) adapter;
            quickAdapter.openLoadAnimation();
            quickAdapter.setLoadMoreView(new PullLoadMoreView());
        }
    }

    /**
     * 设置垂直滚动条可用性
     *
     * @param enable
     */
    public void setRecyclerVerticalScrollBarEnable(boolean enable) {
        mRecyclerView.setVerticalScrollBarEnabled(enable);
    }

    /**
     * 加载完成所有数据
     */
    public void loadMoreEnd() {
        if (isQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).loadMoreEnd(true);
        }
    }

    /**
     * 加载完成当页数据
     */
    public void loadMoreComplete() {
        if (isQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).loadMoreComplete();
        }
    }

    /**
     * 加载失败
     */
    public void loadMoreFail() {
        if (isQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).loadMoreFail();
        }
    }

    /**
     * 设置新数据
     *
     * @param data
     */
    public void setNewData(List data) {
        if (isQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).setNewData(data);
        }
    }

    /**
     * 添加新数据
     *
     * @param newData
     */
    public void addData(List newData) {
        if (isQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).addData(newData);
        }
    }

    /**
     * 返回列表数据
     *
     * @return
     */
    public List getData() {
        if (isQuickAdapter) {
            List dataList = ((BaseQuickAdapter) mRecyclerView.getAdapter()).getData();
            if (dataList != null) {
                return dataList;
            }
        }
        return new ArrayList();
    }

    /**
     * 设置加载更多监听器
     *
     * @param listener
     */
    public void setOnLoadMoreListener(BaseQuickAdapter.RequestLoadMoreListener listener) {
        if (isQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).setOnLoadMoreListener(listener, mRecyclerView);
        }
    }

    /**
     * 设置数据项点击事件
     *
     * @param listener
     */
    public void setOnItemClickListener(BaseQuickAdapter.OnItemClickListener listener) {
        if (isQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).setOnItemClickListener(listener);
        }
    }

    /**
     * 通知数据集刷新
     */
    public void notifyDataSetChanged() {
        mRecyclerView.getAdapter().notifyDataSetChanged();
    }

    /**
     * 设置Post刷新状态
     *
     * @param refreshing
     */
    public void setPostRefreshing(final boolean refreshing) {
        post(new Runnable() {
            @Override
            public void run() {
                setRefreshing(refreshing);
            }
        });
    }

    /**
     * 第一页禁止加载更多
     */
    public void disableLoadMoreIfNotFullPage() {
        if (isQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).disableLoadMoreIfNotFullPage();
        }
    }

    /**
     * 设置EmptyView
     */
    public void setPullEmptyView() {
        if (isQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter())
                    .setEmptyView(R.layout.layout_empty_view, this);
        }
    }

    public RecyclerView getmRecyclerView() {
        return mRecyclerView;
    }
}
